package org.appfuse.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.appfuse.dao.hibernate.BaseDaoHibernate;
import org.appfuse.model.Criteria;
import org.appfuse.dao.CriteriaDao;
import org.hibernate.FlushMode;

import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate.HibernateAccessor;

public class CriteriaDaoHibernate extends BaseDaoHibernate implements CriteriaDao {

	/**
	 * @see org.appfuse.dao.CriteriaDao#getCriterias(org.appfuse.model.Criteria)
	 */
	public List getCriterias(final Criteria criteria) {
		// return getHibernateTemplate().find("from Criteria");

		if (criteria == null || StringUtils.isBlank(criteria.getCriteriaNumber()) || criteria.getCriteriaNumber() == null) {
			return getHibernateTemplate().find("from Criteria where parentNumber is null");
		}
		List criterias = getHibernateTemplate().find("from Criteria where parentNumber = '" + criteria.getCriteriaNumber() + "' order by criterianumber");
		return getHibernateTemplate().find("from Criteria where parentNumber = '" + criteria.getCriteriaNumber() + "' order by criterianumber");

		/*
		 * Remove the line above and uncomment this code block if you want to
		 * use Hibernate's Query by Example API. if (Criteria == null) { return
		 * getHibernateTemplate().find("from Criteria"); } else { // filter on
		 * properties set in the Criteria HibernateCallback callback = new
		 * HibernateCallback() { public Object doInHibernate(Session session)
		 * throws HibernateException { Example ex =
		 * Example.create(Criteria).ignoreCase().enableLike(MatchMode.ANYWHERE);
		 * return session.createCriteria(Criteria.class).add(ex).list(); } }; return
		 * (List) getHibernateTemplate().execute(callback); }
		 */
	}
	
	public List getAllCriterias(){
		return getHibernateTemplate().find("from Criteria order by criterianumber");
	}

	/**
	 * @see org.appfuse.dao.CriteriaDao#getCriteria(Long id)
	 */
	public Criteria getCriteria(final Long id) {
		Criteria criteria = (Criteria) getHibernateTemplate().get(Criteria.class, id);
		if (criteria == null) {
			log.warn("uh oh, criteria with id '" + id + "' not found...");
			throw new ObjectRetrievalFailureException(Criteria.class, id);
		}

		return criteria;
	}

	/**
	 * @see org.appfuse.dao.CriteriaDao#saveCriteria(Criteria Criteria)
	 */
	public void saveCriteria(final Criteria criteria) {
		//getHibernateTemplate().saveOrUpdate(criteria);
		getHibernateTemplate().save(criteria);
		getHibernateTemplate().flush();
	}
	
	public void updateCriteria(Criteria criteria){
		//getHibernateTemplate().setFlushMode(HibernateAccessor.FLUSH_AUTO);
		getSession().setFlushMode(FlushMode.AUTO);
		getHibernateTemplate().update(criteria);
		getHibernateTemplate().flush();
	}

	/**
	 * @see org.appfuse.dao.CriteriaDao#removeCriteria(Long id)
	 */
	public void removeCriteria(final Long id) {
		Criteria criteria = getCriteria(id);
		//get criteria and its children
		List criterias = new ArrayList(); 
		if (criteria != null && criteria.getCriteriaNumber() != null && !"".equals(criteria.getCriteriaNumber())){
			criterias = getCriterias(criteria);
		}
		criterias.add(criteria);
		getHibernateTemplate().deleteAll(criterias);
		
	}
}
